package ncu.me.musicplayer.sql;


import java.util.Date;
import java.util.List;

import ncu.me.musicplayer.MyApplication;
import ncu.me.musicplayer.entity.CollectInfo;
import ncu.me.musicplayer.entity.CollectInfoDao;

/**
 * @Author ye
 * @Date 2020/6/2 20:29
 * @Version 1.0
 */
public class CollectDao {
    CollectInfoDao mDao;
    private CollectDao(){
        mDao = MyApplication.sDaoSession.getCollectInfoDao();
    }
    public static CollectDao getInstance(){return CollectDao.Holder.instance;}
    static class Holder{
        static CollectDao instance = new CollectDao();
    }
    public List<CollectInfo> getAll(){
        return mDao.queryBuilder().orderDesc(CollectInfoDao.Properties.Time).list();
    }
    public boolean isHas(Long musicId){
        return mDao.queryBuilder().where(CollectInfoDao.Properties.MusicId.eq(musicId)).count()>0;
    }
    public void insert(Long musicId){
        try {
            if (isHas(musicId)){
                return;
            }
            CollectInfo info = new CollectInfo();
            info.setMusicId(musicId);
            info.setTime(new Date().getTime());
            mDao.insert(info);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public void delete(Long id){
        mDao.deleteByKey(id);
    }
    public void deleteByMusicId(Long musicId){
        try{
            mDao.queryBuilder()
                    .where(CollectInfoDao.Properties.MusicId.eq(musicId))
                    .buildDelete()
                    .executeDeleteWithoutDetachingEntities();
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
